package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportManageRoleRelRpcService;
import com.hunttown.mes.rpc.domain.TransportManageRoleRelDTO;
import com.hunttown.mes.rpc.domain.query.TransportManageRoleRelDTOQuery;
import com.hunttown.mes.service.TransportManageRoleRelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportManageRoleRelConverts.convert;

/**
 * created by wangjunfu on 2022-01-28 11:16:43
 */
@Service
public class TransportManageRoleRelRpcServiceImpl implements TransportManageRoleRelRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportManageRoleRelRpcServiceImpl.class);

    private final TransportManageRoleRelService transportManageRoleRelService;

    @Autowired
    public TransportManageRoleRelRpcServiceImpl(TransportManageRoleRelService transportManageRoleRelService) {
        this.transportManageRoleRelService = transportManageRoleRelService;
    }

    @Override
    public TransportManageRoleRelDTO insert(TransportManageRoleRelDTO transportManageRoleRelDTO) {
        try {
            return convert(transportManageRoleRelService.insert(convert(transportManageRoleRelDTO)));
        } catch (Exception e) {
            logger.error("TransportManageRoleRel create failed.", e);
            throw new RuntimeException("TransportManageRoleRel create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportManageRoleRelDTO transportManageRoleRelDTO) {
        try {
            return transportManageRoleRelService.updateInfoById(convert(transportManageRoleRelDTO));
        } catch (Exception e) {
            logger.error("TransportManageRoleRel update by id failed.", e);
            throw new RuntimeException("TransportManageRoleRel update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportManageRoleRelDTOQuery transportManageRoleRelDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportManageRoleRelDTOQuery.getW_id() != null && transportManageRoleRelDTOQuery.getW_id() > 0) {
                query.put("w_id", transportManageRoleRelDTOQuery.getW_id());
            }

            if (transportManageRoleRelDTOQuery.getIds() != null && !transportManageRoleRelDTOQuery.getIds().equals("")) {
                query.put("ids", transportManageRoleRelDTOQuery.getIds());
            }

            if (transportManageRoleRelDTOQuery.getW_tableId() != null) {
                query.put("w_tableId", transportManageRoleRelDTOQuery.getW_tableId());
            }

            if (transportManageRoleRelDTOQuery.getW_recordId() != null) {
                query.put("w_recordId", transportManageRoleRelDTOQuery.getW_recordId());
            }

            if (transportManageRoleRelDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportManageRoleRelDTOQuery.getDeleteFlag());
            }

            return transportManageRoleRelService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportManageRoleRel update by query failed.", e);
            throw new RuntimeException("TransportManageRoleRel update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportManageRoleRelDTO selectById(Integer id) {
        try {
            return convert(transportManageRoleRelService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportManageRoleRel select by id failed.", e);
            throw new RuntimeException("TransportManageRoleRel select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportManageRoleRelDTO selectObjByQuery(TransportManageRoleRelDTOQuery transportManageRoleRelDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportManageRoleRelDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportManageRoleRelDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportManageRoleRelDTOQuery.getOrderBy())) {
                query.put("orderBy", transportManageRoleRelDTOQuery.getOrderBy());
            }

            return convert(transportManageRoleRelService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportManageRoleRel select obj by query failed.", e);
            throw new RuntimeException("TransportManageRoleRel select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportManageRoleRelDTO> getListForPage(TransportManageRoleRelDTOQuery transportManageRoleRelDTOQuery) {
        Query query = Query.page(transportManageRoleRelDTOQuery.getPn(), transportManageRoleRelDTOQuery.getPageSize());

        if (transportManageRoleRelDTOQuery.getId() != null && transportManageRoleRelDTOQuery.getId() > 0) {
            query.put("id", transportManageRoleRelDTOQuery.getId());
        }

        if (transportManageRoleRelDTOQuery.getIds() != null && !transportManageRoleRelDTOQuery.getIds().equals("")) {
            query.put("ids", transportManageRoleRelDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportManageRoleRelDTOQuery.getLikeName())) {
            query.put("likeName", transportManageRoleRelDTOQuery.getLikeName());
        }

        if (transportManageRoleRelDTOQuery.getTableId() != null) {
            query.put("tableId", transportManageRoleRelDTOQuery.getTableId());
        }

        if (transportManageRoleRelDTOQuery.getRecordId() != null) {
            query.put("recordId", transportManageRoleRelDTOQuery.getRecordId());
        }

        if (transportManageRoleRelDTOQuery.getRoleId() != null) {
            query.put("roleId", transportManageRoleRelDTOQuery.getRoleId());
        }

        if (StringUtils.isNotBlank(transportManageRoleRelDTOQuery.getShowDate())) {
            query.put("showDate", transportManageRoleRelDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportManageRoleRelDTOQuery.getStartDate())) {
            query.put("startDate", transportManageRoleRelDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportManageRoleRelDTOQuery.getEndDate())) {
            query.put("endDate", transportManageRoleRelDTOQuery.getEndDate());
        }

        if (transportManageRoleRelDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportManageRoleRelDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(transportManageRoleRelDTOQuery.getOrderBy())) {
            query.put("orderBy", transportManageRoleRelDTOQuery.getOrderBy());
        }

        List<TransportManageRoleRelDTO> list = convert(transportManageRoleRelService.selectByQuery(query));
        Page<TransportManageRoleRelDTO> page = Page.from(transportManageRoleRelDTOQuery.getPn(), transportManageRoleRelDTOQuery.getPageSize(), list);
        page.setTotalItems(transportManageRoleRelService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportManageRoleRelService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportManageRoleRel delete failed.", e);
            throw new RuntimeException("TransportManageRoleRel delete failed, Please order your code.", e);
        }
    }
}